package com.protol;

import android.content.Intent;
import android.os.Bundle;
import com.helloandroid.WelcomeActivity;
import com.helloandroid.dialogs.YinSiDialog;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 用户协议和动态权限界面。
 * 获取广告之前需要同意用户协议和授权READ_PHONE_STATE权限
 */
public class ProtocolActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar bar = getSupportActionBar();
        if (bar != null){
            bar.hide();
        }
        initProtocol();
    }

    private void initProtocol() {
        // 是否同意用户协议
        SharedInfoService shareInfoService = SharedInfoService.getInstance(this);

        // 未同意用户协议，则显示用户协议弹框
        if (!shareInfoService.getIsAgreeProtocol()) {
            showProtocol();
        } else {
            enterSplash();
        }
    }

    /**
     * 显示协议框
     */
    private void showProtocol() {

        YinSiDialog dialog = new YinSiDialog();
        dialog.show(getSupportFragmentManager());

//        LayoutInflater inflate = LayoutInflater.from(this);
//        View view = inflate.inflate(R.layout.protocol_dialog_content, null);
//
//        // 显示用户协议弹框
//        ProtocolDialog dialog = new ProtocolDialog(this, getString(R.string.users_note), view);
//        dialog.setCallback(this);
//        dialog.setICloseDlgListener(this);
//        dialog.setCanceledOnTouchOutside(false);
//
//        // 屏蔽 back 按键
//        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                return true;
//            }
//        });
//        dialog.show();
//
//
//        TextView agreeTip = (TextView) dialog.findViewById(R.id.agree_tip);
//
//
//        String string = getResources().getString(R.string.privacy_tips);
//        String key1 = "《服务协议》";
//        String key2 = "《隐私政策》";
//        int index1 = string.indexOf(key1);
//        int index2 = string.indexOf(key2);
//
//        //需要显示的字串
//        SpannableString spannedString = new SpannableString(string);
//        //设置点击字体颜色
//        @SuppressLint("ResourceAsColor") ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(R.color.underLine);
//        spannedString.setSpan(colorSpan1, index1, index1 + key1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//        @SuppressLint("ResourceAsColor") ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(R.color.underLine);
//        spannedString.setSpan(colorSpan2, index2, index2 + key2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
////        //设置点击字体大小
////        AbsoluteSizeSpan sizeSpan1 = new AbsoluteSizeSpan(18, true);
////        spannedString.setSpan(sizeSpan1, index1, index1 + key1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
////        AbsoluteSizeSpan sizeSpan2 = new AbsoluteSizeSpan(18, true);
////        spannedString.setSpan(sizeSpan2, index2, index2 + key2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//        //设置点击事件
//        ClickableSpan clickableSpan1 = new ClickableSpan() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(ProtocolActivity.this, TermsActivity.class);
//                startActivity(intent);
//            }
//
////            @Override
////            public void updateDrawState(TextPaint ds) {
////                //点击事件去掉下划线
////                ds.setUnderlineText(true);
////            }
//        };
//        spannedString.setSpan(clickableSpan1, index1, index1 + key1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//
//        ClickableSpan clickableSpan2 = new ClickableSpan() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(ProtocolActivity.this, PrivacyPolicyActivity.class);
//                startActivity(intent);
//            }
//        };
//        spannedString.setSpan(clickableSpan2, index2, index2 + key2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//
//        //设置点击后的颜色为透明，否则会一直出现高亮
//        agreeTip.setHighlightColor(Color.TRANSPARENT);
//        //开始响应点击事件
//        agreeTip.setMovementMethod(LinkMovementMethod.getInstance());
//
//        agreeTip.setText(spannedString);
    }

    /**
     * 同意用户协议，重新进入开屏界面
     */
    private void enterSplash() {
        startActivity(new Intent(this, WelcomeActivity.class));
        this.finish();
    }

//    @Override
//    public void okCallback(boolean showAd) {
//        // 同意用户协议后，继续动态权限弹框
//        hanldeRuntimePermission();
//    }

//    @Override
//    public void cancelCallback() {
//        finish();
//    }
//
//    @Override
//    public void onCloseDlg() {
//        finish();
//    }

    public void hanldeRuntimePermission() {

        SharedInfoService.getInstance(this).setIsAgreeProtocl(true);

        // PPS需要采集IMEI，申请READ_PHONE_STATE权限
        if (PermissionUtil.verifyPermissions(this, PermissionUtil.PERMISSIONS)) {
            enterSplash();
        } else {
            String[] pl = PermissionUtil.getDenyPermissions(this, PermissionUtil.PERMISSIONS);
            if (null == pl || 0 == pl.length) {
                finish();
            } else {
                PermissionUtil.requestPermissions(this, pl, PermissionUtil.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS_INIT);
            }
        }
    }

    // 处理权限回调结果
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PermissionUtil.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS_INIT: {
                startActivity(new Intent(this, WelcomeActivity.class));
                break;
            }
            default: {
                break;
            }
        }
        finish();
    }

}
